extends Node2D

# 蘑菇效果属性
@export var effect_type: int = 0  # 0=攻击, 1=速度, 2=生命
@export var effect_duration: float = 10.0  # 效果持续时间
@export var effect_strength: float = 1.2  # 效果强度

var time_alive: float = 0.0
var is_collected: bool = false
var player = null

func _ready():
	# 获取玩家引用
	player = get_tree().get_first_node_in_group("player")
	
	# 设置外观
	match effect_type:
		0:  # 攻击
			$Sprite2D.modulate = Color(1.0, 0.5, 0.5)  # 红色
		1:  # 速度
			$Sprite2D.modulate = Color(0.5, 0.5, 1.0)  # 蓝色
		2:  # 生命
			$Sprite2D.modulate = Color(0.5, 1.0, 0.5)  # 绿色
	
	# 设置碰撞区域
	$Area2D/CollisionShape2D.shape.radius = 30.0
	
	# 连接信号
	$Area2D.connect("body_entered", _on_body_entered)
	
	# 添加动画效果
	var tween = create_tween()
	tween.set_loops()
	tween.tween_property($Sprite2D, "scale", Vector2(1.2, 1.2), 0.5)
	tween.tween_property($Sprite2D, "scale", Vector2(1.0, 1.0), 0.5)

func _process(delta):
	# 更新生命周期
	time_alive += delta
	
	# 到达最大存活时间后消失
	if time_alive >= 30.0 and not is_collected:
		# 淡出效果
		var tween = create_tween()
		tween.tween_property(self, "modulate:a", 0.0, 1.0)
		tween.tween_callback(queue_free)

func _on_body_entered(body):
	# 检查是否是玩家
	if body.is_in_group("player") and not is_collected:
		is_collected = true
		
		# 应用效果
		apply_effect(body)
		
		# 播放收集动画
		var tween = create_tween()
		tween.tween_property(self, "scale", Vector2(1.5, 1.5), 0.2)
		tween.tween_property(self, "scale", Vector2(0.0, 0.0), 0.3)
		tween.tween_callback(queue_free)
		
		# 播放音效（如果有）
		if has_node("AudioStreamPlayer2D"):
			$AudioStreamPlayer2D.play()

func apply_effect(player_body):
	# 根据效果类型应用不同效果
	match effect_type:
		0:  # 攻击
			if player_body.has_method("apply_damage_boost"):
				player_body.apply_damage_boost(effect_strength, effect_duration)
			else:
				# 备用方法
				player_body.damage *= effect_strength
				get_tree().create_timer(effect_duration).connect("timeout", _on_effect_timeout.bind(player_body, "damage", player_body.damage / effect_strength))
		
		1:  # 速度
			if player_body.has_method("apply_speed_boost"):
				player_body.apply_speed_boost(effect_strength, effect_duration)
			else:
				# 备用方法
				player_body.speed *= effect_strength
				get_tree().create_timer(effect_duration).connect("timeout", _on_effect_timeout.bind(player_body, "speed", player_body.speed / effect_strength))
		
		2:  # 生命
			if player_body.has_method("heal"):
				var heal_amount = int(player_body.max_health * 0.3)  # 恢复30%最大生命值
				player_body.heal(heal_amount)
	
	# 显示效果提示
	var effect_name = ""
	match effect_type:
		0: effect_name = "攻击提升!"
		1: effect_name = "速度提升!"
		2: effect_name = "生命恢复!"
	
	# 创建浮动文本
	var floating_text = preload("res://FloatingText.tscn").instantiate()
	get_tree().get_root().add_child(floating_text)
	floating_text.global_position = global_position
	floating_text.text = effect_name
	floating_text.color = $Sprite2D.modulate

func _on_effect_timeout(player_body, property, original_value):
	# 恢复原始属性值
	if is_instance_valid(player_body):
		player_body.set(property, original_value)
